<template>
  <div>
    <el-dialog
      :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form :model="dataForm"  label-width="auto"
               :rules="dataRule"
               ref="dataForm">
        <el-form-item label="类型">
          <el-radio v-model="dataForm.sellerType" :label="1">酒店</el-radio>
          <el-radio v-model="dataForm.sellerType" :label="2">服务商</el-radio>
        </el-form-item>
        <el-form-item label="搜索酒店" prop="sellerId" v-if="dataForm.sellerType === 1">
          <el-select
            style="width:100%;"
            v-model="dataForm.sellerId"
            filterable
            remote
            reserve-keyword
            @change="selectSeller"
            placeholder="请输入联系人/电话"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option
              v-for="item in platformSeller"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="搜索服务商" prop="sellerId" v-if="dataForm.sellerType === 2">
          <el-select
            style="width:100%;"
            v-model="dataForm.sellerId"
            filterable
            remote
            reserve-keyword
            @change="selectSeller"
            placeholder="请输入联系人/电话"
            :remote-method="remoteMethodFacilitator"
            :loading="loading"
          >
            <el-option
              v-for="item in platformSeller"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="类型">
          <el-radio v-model="dataForm.type" :label="1">支出</el-radio>
          <el-radio v-model="dataForm.type" :label="2">收入</el-radio>
        </el-form-item>
        <el-form-item label="支付类型">
          <el-radio v-model="dataForm.payType" :label="1">银行卡</el-radio>
          <el-radio v-model="dataForm.payType" :label="2">支付宝</el-radio>
          <el-radio v-model="dataForm.payType" :label="3">微信</el-radio>
          <el-radio v-model="dataForm.payType" :label="4">其他</el-radio>
        </el-form-item>
        <el-form-item label="提现金额" prop="amount">
          <el-input type="number" v-model="dataForm.amount" :min="0" :max="10000">
            <el-button slot="append">元</el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
    </el-dialog>

    <gallery
      :images="[showImage]"
      :index="index"
      @close="index = null"
    />
  </div>
</template>

<script>
import VueGallery from 'vue-gallery';

export default {
  components: {
    'gallery': VueGallery
  },
  data() {
    return {
      loading:false,
      visible: false,
      readonly:false,
      platformSeller:[],
      dataForm: {
        sellerId:"",
        type:1,
        sellerType:1,
        payType: 1,
        amount: 0
      },
      dataRule: {
        sellerId: [
          { required: true, message: "商家不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "费用不能为空", trigger: "blur" }
        ]
      },
      showImage: '',
      index: null
    };
  },
  methods: {
    selectSeller(id,value) {
      const seller = this.platformSeller.find(m => m.id === id)
      this.dataForm.sellerName = seller.name;
      this.dataForm.sellerId = seller.id;
    },
    //加载平台商家数据
    remoteMethod(query) {
      if(this.isAuth('hotel:hotelseller:list')){
        this.loading = true;
        this.$http({
          url: this.$http.adornUrl("/hotel/hotelseller/platformSeller"),
          method: "get",
          params: this.$http.adornParams({
            sellerName: query
          })
        }).then(({ data }) => {
          this.loading = false;
          if (data && data.code === 0) {
            this.platformSeller = data.page.list;
          } else {
            this.$message.error(data.msg);
          }
        });
      }
    },
    remoteMethodFacilitator(query) {
      this.loading = true;
      this.$http({
        url: this.$http.adornUrl("/hotel/facilitator/platformFacilitatorsr"),
        method: "get",
        params: this.$http.adornParams({
          name: query
        })
      }).then(({ data }) => {
        this.loading = false;
        if (data && data.code === 0) {
          this.platformSeller = data.page.list;
        } else {
          this.$message.error(data.msg);
        }
      });
    },

    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelOfflineTransaction/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.hotelOfflineTransaction;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelOfflineTransaction/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              ...this.dataForm
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }

  }
};
</script>
